package org.demo.tomcat;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author  
 * @date    2010-12-11
 * @file    org.demo.tomcat.AjaxServer.java
 */
public class AjaxXml extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 1. 取参数
		String username = req.getParameter("userName");
		resp.setContentType("text/xml;charset=utf-8");
		PrintWriter out = resp.getWriter();
		// 2. 检查参数是否正确
		out.println("<message>");
		if (username == null || username.length() == 0){
			out.println("用户名不能为空！");
		} else {
			if (username.equals("dycc")){
				out.println("用户名 [dycc] 已存在!");
			} else {
				out.println("用户名 [" + username + "]未存在可以使用.");
			}
		}
		out.println("</message>");
		// 3. 返回结果
		out.flush();
		out.close();
	}
}
